//移动端全局对象
var mobileGlobal = {
	messageToast : null, 
	successToast : null, 
	failedToast : null, 
	loadingToast : null, 
	messageDialog : null, 
	confirmDialog : null, 
	init : function() {		//初始化
		this.messageToast = $("#messageToast");
		this.successToast = $("#successToast");
		this.failedToast = $("#failedToast");
		this.loadingToast = $("#loadingToast");
		this.messageDialog = $("#messageDialog");
		this.confirmDialog = $("#confirmDialog");
	}, 
	forward : function(url, cache, params) {	//前进
		if (cache) {
			var histories = window.sessionStorage.getItem("access_history_url");
			if (histories) {
				histories = histories.split(",");
				histories.push(escape(window.location.href));
			} else histories = [escape(window.location.href)];
			
			window.sessionStorage.setItem("access_history_url", histories.join(","));
			if (params) window.sessionStorage.setItem("access_history_params_" + (histories.length - 1), params);
			else window.sessionStorage.removeItem("access_history_params_" + (histories.length - 1));
		}
		
		window.location.replace(url);
	}, 
	backword : function() {	//后退
		var histories = window.sessionStorage.getItem("access_history_url");
		if (!histories) return;
		
		histories = histories.split(",");
		
		var url = unescape(histories.pop());
		window.sessionStorage.setItem("access_history_url", histories.join(","));
		
		window.location.replace(url);
	}, 
	getPageParams : function() {	//获取页面参数
		var histories = window.sessionStorage.getItem("access_history_url");
		if (!histories) {
			var params = window.sessionStorage.getItem("access_history_params_0");
			window.sessionStorage.removeItem("access_history_params_0");
			return params;
		}
		
		histories = histories.split(",");
		var params = window.sessionStorage.getItem("access_history_params_" + histories.length);
		window.sessionStorage.removeItem("access_history_params_" + histories.length);
		return params;
	}, 
	message : function(msg) {	//显示普通消息
		if (this.messageToast.css("display") != "none") return;
		
		this.messageToast.find("p.weui-toast__content").text(msg);
		
		this.messageToast.fadeIn(100);
		setTimeout(function() {
			mobileGlobal.messageToast.fadeOut(100);
		}, 2000);
	}, 
	success : function(msg) {	//显示成功消息
		if (this.successToast.css("display") != "none") return;
		
		this.successToast.find("p.weui-toast__content").text(msg);
		
		this.successToast.fadeIn(100);
		setTimeout(function() {
			mobileGlobal.successToast.fadeOut(100);
		}, 2000);
	}, 
	failed : function(msg) {	//显示失败消息
		if (this.failedToast.css("display") != "none") return;
		
		this.failedToast.find("p.weui-toast__content").text(msg);
		
		this.failedToast.fadeIn(100);
		setTimeout(function() {
			mobileGlobal.failedToast.fadeOut(100);
		}, 2000);
	}, 
	loading : function(msg) {	//显示加载中
		if (this.loadingToast.css("display") != "none") return;
		
		msg = msg || "请稍后..";
		this.loadingToast.find("p.weui-toast__content").text(msg);
		this.loadingToast.fadeIn(100);
	}, 
	hideLoading : function() {	//隐藏加载中
		if (this.loadingToast.css("display") == "none") return;
		this.loadingToast.fadeOut(100);
	}, 
	openMessageDialog : function(message, handler) {	//打开消息对话框
		if (this.messageDialog.css("display") != "none") return;
		
		this.messageDialog.find("div.weui-dialog__bd").text(message);
		
		handler = handler || this.closeMessageDialog;
		var button = this.messageDialog.find("a.weui-dialog__btn");
		button.unbind("click").bind("click", handler);
		
		this.messageDialog.fadeIn(200);
	}, 
	closeMessageDialog : function() {	//关闭消息对话框
		mobileGlobal.messageDialog.fadeOut(200);
	}, 
	openConfirmDialog : function(message, handler) {	//打开确认对话框
		if (this.confirmDialog.css("display") != "none") return;
		
		this.confirmDialog.find("div.weui-dialog__bd").text(message);
		
		handler = handler || this.closeConfirmDialog;
		var button = this.confirmDialog.find("a.weui-dialog__btn:eq(0)");
		button.unbind("click").bind("click", handler);
		
		this.confirmDialog.fadeIn(200);
	}, 
	closeConfirmDialog : function() {	//关闭确认对话框
		mobileGlobal.confirmDialog.fadeOut(200);
	}, 
	sendRequest : function(url, data, loading, success, error) {	//发送请求
		this.loading(loading);
		
		$.ajax({
			url : url,
			data : data,
			success : function(data, status) {
				//请求成功处理
				if (success) success.call(mobileGlobal, data, status);
				else {
					mobileGlobal.hideLoading();
					if (data.status) mobileGlobal.success("操作成功");
					else mobileGlobal.failed("操作失败");
				}
			},
			error : function(xhr, msg, e) {
				//请求错误处理
				if (error) error.call(mobileGlobal, xhr, msg, e);
				else {
					mobileGlobal.hideLoading();
					mobileGlobal.failed("网络请求错误");
				}
			}
		});
	}
};

$(function() {
	mobileGlobal.init();
});
